package com.androidhive.androidlistview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.Toast;

public class AddCrops extends Activity {
	
	SessionManager session;
	AlertDialogManager alert = new AlertDialogManager();
	
	// Email, password edittext
	EditText baseCost,Qty,pkgCost;
	Spinner cropName,cropQuality,state;
	  DatabaseHandler db = new DatabaseHandler(this);
	// login button
	Button submit;
	
	// Alert Dialog Manager
	
	
	// Session Manager Class
	
	
public void addListenerOnSpinnerItemSelection(){
		
		
	cropName = (Spinner) findViewById(R.id.cropname);
	state = (Spinner) findViewById(R.id.state);
	
		//CustomOnItemSelectedListener c = new CustomOnItemSelectedListener();
	cropName.setOnItemSelectedListener(new OnItemSelectedListener() {
	        public void onItemSelected(AdapterView<?> parent, View view, int pos,
	    			long id) {
	        	Toast.makeText(parent.getContext(), 
	    				"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
	    				Toast.LENGTH_SHORT).show();
	           
	            	addItemsOnSpinner2(parent.getItemAtPosition(pos).toString());
//	                Intent intent = new Intent(MyActivity.this, AnotherActivity.class);
//	                MyActivity.this.startActivity(intent);
	            
	        }

	        public void onNothingSelected(AdapterView<?> parentView) {
	            // To do ...
	        }

	    });
		//Log.d("crop:",crop);
		
		
	}
	
	
	public void addItemsOnSpinner2(String s) {

		cropQuality = (Spinner) findViewById(R.id.cropQuality);
		List<String> list = new ArrayList<String>();
		if(s.equalsIgnoreCase("Rice")){
		list.add("Aizon Rice");

		list.add("Champaa Rice");
		list.add("Clearfield Rice");
		list.add("Hasan serai");
		list.add("Basamati Rice");
		list.add("Dubraj Rice");
		list.add("Basamati Rice");
		list.add("Dubraj Rice");

		list.add("HMT Rice");
		list.add("Thimmasamundaram Mollakolukulu");
		list.add("Jay Shrirama Rice");
		list.add("Raja Hansa rice");
		list.add("Pusa Rice");
		list.add("Idly Rice");
		list.add("Basmati tukda");
		
		
		}
		
		else if(s.equalsIgnoreCase("Wheat")){
			list.add("PBW 343");
			list.add("WH 896(d)");
			list.add("PDW 233(d)");
			list.add("PDW 215(d)");
			list.add("WH 542");
			list.add("Raj 3077");
			list.add("UP 2336");
			list.add("CPAN 3004");
			list.add("K 8027");
			list.add("UPC 306");
		}
		else if(s.equalsIgnoreCase("Ground Nut")){
			String[] listing=new String[]{"Kadiri 5", "Kadiri 6" , "Kadiri 9", "GPBD 4", "Narayani", "Abhaya", "Prasuna", "ICGV 91114"};
			for(int i=0;i<listing.length;i++){
				list.add(listing[i]);
			}
		}//
		else if(s.equalsIgnoreCase("Pulses")){
			String[] listing=new String[]{"Chana", "toor/arhar" , "lobiya", "urad", "rajma", "moong", "masoor"};
			for(int i=0;i<listing.length;i++){
				list.add(listing[i]);
			}
		}//
		else if(s.equalsIgnoreCase("Soybean")){
			String[] listing=new String[]{"Ahilya-1", "Ahilya-2" , "Ahilya-3", "NRC 37", "Alankar", "Ankur", "Birsa soy","Bragg","Hara soy"};
			for(int i=0;i<listing.length;i++){
				list.add(listing[i]);
			}
		}//
		
		
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
		dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		cropQuality.setAdapter(dataAdapter);
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		 ScrollView sv = new ScrollView(this);
		  HorizontalScrollView hsv = new HorizontalScrollView(this);
		setContentView(R.layout.activity_add_crops);
		// setContentView(sv); 
		addListenerOnSpinnerItemSelection();
		 session = new SessionManager(getApplicationContext());                
	        
	        // Email, Password input text
		
		 baseCost = (EditText) findViewById(R.id.baseCost);
		 Qty = (EditText) findViewById(R.id.quantity); 
	      pkgCost=  (EditText) findViewById(R.id.pkgCost); 
	      //  Toast.makeText(getApplicationContext(), "User Login Status: " + session.isLoggedIn(), Toast.LENGTH_LONG).show();
	        
	        
	        // Login button
	        submit = (Button) findViewById(R.id.submit);
	        submit.setOnClickListener(new View.OnClickListener() {
				
				//@Override
				public void onClick(View arg0) {
					// Get username, password from EditText
					String  cropname= String.valueOf(cropName.getSelectedItem());
					String cropquality =String.valueOf(cropQuality.getSelectedItem());
					String State=String.valueOf(state.getSelectedItem());
					String basecost=baseCost.getText().toString();
					String pkgcost=pkgCost.getText().toString();
					String qty=Qty.getText().toString();
					 Intent intent = getIntent();
					  //  String CropIdnew = intent.getStringExtra(MyWebViewClient.cropId);
					  //  session.logoutUser();
					  //  session.createLoginSessionCropId(CropIdnew);
					    HashMap<String, String> user = session.getUserDetails();
				      
				        String farmerId = (user.get(SessionManager.Vendor_Id)); 
				       // String CropId = user.get(SessionManager.Crop_Id);
				        Log.d("farmer:",farmerId+" "+cropname+" "+cropquality+" "+basecost+" "+qty);
				       // Log.d("Crop:HK",CropId );
				        
					
					// Check if username, password is filled				
					if(cropname.trim().length()<=0 ||pkgcost.trim().length()<=0 || cropquality.trim().length()<=0 || basecost.trim().length()<=0 || qty.trim().length()<=0 ){
						
							alert.showAlertDialog(AddCrops.this, "Crop storage failed..", "Please enter all the entries.", false);
					
					}	
						
						else{
//						
							String fname=db.getFname(Integer.parseInt(farmerId));
							//void addToMastercroptableByFarmer(String cropname, int farmerId, String cropQuality,int cost, int baseCost, String fname, int quantity) 
							
							db.addToMastercroptableByFarmer(cropname, Integer.parseInt(farmerId), cropquality,  Integer.parseInt(basecost),  Integer.parseInt(basecost),Integer.parseInt(pkgcost), fname, State, Integer.parseInt(qty));		
							
							AlertDialog.Builder alt_bld1 = new AlertDialog.Builder(AddCrops.this);
						    alt_bld1.setMessage("Do you want to close this application ?")
						    .setCancelable(false)
						    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
						    public void onClick(DialogInterface dialog, int id) {
						    	
						    	AlertDialog.Builder alt_bld = new AlertDialog.Builder(AddCrops.this);
							    alt_bld.setMessage("Do you want to close this application ?")
							    .setCancelable(false)
							    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
							    public void onClick(DialogInterface dialog, int id) {
							    	Intent i = new Intent(getApplicationContext(), AddCrops.class);
									startActivity(i);
										finish();
							    	
										
									 
									   
							    	
							    }
							    })
							    .setNegativeButton("No", new DialogInterface.OnClickListener() {
							    public void onClick(DialogInterface dialog, int id) {
							    	Intent i = new Intent(getApplicationContext(), FarmerMenu.class);
									startActivity(i);
										finish();
							  
							    	
							}
							    });
							    AlertDialog alert = alt_bld.create();
							    // Title for AlertDialog
							 //   alert.setTitle("Terms And Conditions");
							    alert.setMessage("Do you want to add more Crops?");
							    // Icon for AlertDialog
							    alert.setIcon(R.drawable.success);
							    alert.show();
								
						    	
						    }
						    });
						 
						    AlertDialog alert = alt_bld1.create();
						    // Title for AlertDialog
						    alert.setTitle("Congratulations!!!!!");
						    alert.setMessage("Your Crop Details has been recorded successfully." +
						    		"Thank you for using FarmerVendor Collaberation  application." +
						    		"You will be informed regarding Bid related Information.");
						    // Icon for AlertDialog
						    alert.setIcon(R.drawable.success);
						    alert.show();
							
					//yaha			  
						
					}
				
				}
});
	       
	}
}
